package com.happy_hao.msg.interceptor;


import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LogInfo logInfo = new LogInfo();
        //设置请求路径，比如 /user/user/create
         logInfo.setUrl(request.getRequestURI());
        logInfo.setTime(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
//      logInfo.setUserName(request.getParameter("username"));
//      logInfo.setHeaders(request.getHeader("user_token"));
        //获取请求体里面的数据
//      String requestBody = request.getReader().readLine();
        logInfo.setMicroService( "msg");
        //转化为json
        String s = new ObjectMapper().writeValueAsString(logInfo);
        log.info(s);
        return true;
    }
}
